package com.scheduler.actor.strategy;

import com.scheduler.servicediscover.Worker;

import java.util.List;
import java.util.Objects;

/**
 * 轮询，但不包含当前主机，当集群数量为大于等于3时，会生效，否则和普通轮询一样
 */
public class RoundRobinWithoutLocalhostRouterStrategy extends RoundRobinRouterStrategy {

    @Override
    public Worker next(List<Worker> workerList, String localhostIp) {
        Worker next = super.next(workerList, localhostIp);
        if (next == null) {
            return null;
        }
        // 3台以下不开启
        if (workerList.size() < 3) {
            return next;
        }
        // 本地IP为空则直接返回
        if (localhostIp == null) {
            return next;
        }
        // 如果不是本地IP则返回
        if (!Objects.equals(next.getIpAddr(), localhostIp)) {
            return next;
        } else {
            // 如果是本地IP则重新算一次
            return this.next(workerList, localhostIp);
        }
    }
}
